<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'员工管理'),
	array('op'=>'edit','title'=>'添加/编辑员工'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['status']){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),		
	);
	$list=$this->get_list('member',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['agent']=cfc('core')->member_show($item['fromuser']);
			$item['ab']=cfc('core')->member_show($item['gei_mid']);
			$item['url']=custom_url('jy_weishop/index//fromuser='.$item['mid'],true);
			$item['appurl']='jy_weishop/index//fromuser='.$item['mid'];
			$item['commission_1']=$this->sum('commission',array('merchid'=>$_SESSION['onemerchid'],'status <='=>1,'mid'=>$item['mid']),'money');
			$item['commission_2']=$this->sum('commission',array('merchid'=>$_SESSION['onemerchid'],'status'=>2,'mid'=>$item['mid']),'money');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'list'=>array('data'=>'list','tip'=>'【温馨提示】：</br>1、所有员工的分红，可以等级里面设置或独立商品里面设置！<red>修改部门，不影响已经产生的部门业绩</red>','list'=>array(
			array('name'=>'mid','title'=>'MID','type'=>'string'),
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			//array('name'=>'agent','title'=>'上级信息','type'=>'member'),
			array('name'=>'title','title'=>'员工名称','type'=>'string'),
			array('name'=>'level_id','title'=>'员工等级','type'=>'select','options'=>'levels'),
			array('name'=>'bumenid','title'=>'员工部门','type'=>'select','options'=>'bumens'),
			array('name'=>'wechat','title'=>'员工微信号','type'=>'string'),
			array('name'=>'mobile','title'=>'员工手机','type'=>'string'),
			array('type'=>'radio','name'=>'status','title'=>'状态','options'=>'agent_status'),
		),'action'=>array(
		)),
		'search'=>array(
			array('type'=>'radio','width'=>15,'name'=>'status','title'=>'状态','options'=>'agent_status'),
			array('type'=>'choose','width'=>18,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
		),
	);
	$return['htmls']=$htmls;
	$return['agent_status']=$this->agent_status;
	$return['levels']=$this->getall('level',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['bumens']=$this->getall('bumen',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=$this->get('member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array(
			'title'=>'',
		);
	}
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'员工设置',
		'edit'=>array(
			array('name'=>'title','title'=>'员工名称','type'=>'string'),
			array('type'=>'choose','action'=>'core/member.get_member/','name'=>'mid','title'=>'绑定会员'),
			array('name'=>'level_id','title'=>'员工等级','type'=>'select','options'=>'levels'),
			array('name'=>'bumenid','title'=>'员工部门','type'=>'select','options'=>'bumens'),
			array('name'=>'wechat','title'=>'员工微信号','type'=>'string'),
			array('name'=>'mobile','title'=>'员工手机','type'=>'string'),
			array('name'=>'remark','title'=>'备注说明','type'=>'text'),
			array('type'=>'radio','name'=>'status','title'=>'状态','options'=>'agent_status'),
		),
	);
	$return['item']=$item;
	$return['htmls']=$htmls;
	$return['agent_status']=$this->agent_status;
	$return['levels']=$this->getall('level',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['bumens']=$this->getall('bumen',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		'mid'=>trim($postdata['mid']),
		'level_id'=>trim($postdata['level_id']),
		'bumenid'=>trim($postdata['bumenid']),
		'wechat'=>trim($postdata['wechat']),
		'mobile'=>trim($postdata['mobile']),
		'remark'=>trim($postdata['remark']),
		'status'=>trim($postdata['status']),
	);
	if(!$data['title']){
		exi('请填写名称','error');
	}
	$id=intval($postdata['id']);
	if($id){
		$this->update('member',$data,array('id'=>$id));
		exi('设置成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$_SESSION['onemerchid'];
		$this->insert('member',$data);
		exi('添加成功！','','list');
	}
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请选择员工！','error');
	}
	$post=get_postdata();
	$this->update('member',array('status'=>$post['status']),array('id'=>$id));	
	exi('设置成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		$this->delete('member',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$this->delete('member',array('id'=>$id));	
			}
		}
	}
	exi('删除成功！');
}
?>